package com.myapplication.ui.infofragment;

import android.os.Handler;
import android.os.Looper;

import com.myapplication.bean.ZiXunBean;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by zitaojiang on 2016/10/6.
 */
public class Presenter_Info implements Contract_Info.Presenter{
    Handler handler=new Handler(Looper.getMainLooper());
    private Contract_Info.Model mModel;
    private Contract_Info.View mView;

    public Presenter_Info(Contract_Info.View view){
        this.mView=view;
        mModel=new Model_Info();
    }
    @Override
    public void GetData(Map<String, String> params) {
        mModel.getData(params, new Callback<ZiXunBean>() {
            @Override
            public void onResponse(Call<ZiXunBean> call, Response<ZiXunBean> response) {

                ZiXunBean ziXunBean=response.body();
                final List<ZiXunBean.DataBean.ListBean> ziXunBeanList=ziXunBean.getData().getList();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mView.onGetDataSuccess(ziXunBeanList);
                    }
                });
            }

            @Override
            public void onFailure(Call<ZiXunBean> call, Throwable t) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mView.onGetDataFail("网络连接失败");
                    }
                });
            }
        });
    }

    @Override
    public void GetMoreData(Map<String, String> params2) {
        mModel.getMoreData(params2, new Callback<ZiXunBean>() {
            @Override
            public void onResponse(Call<ZiXunBean> call, Response<ZiXunBean> response) {
                ZiXunBean ziXunBean=response.body();
                final List<ZiXunBean.DataBean.ListBean> ziXunBeanList=ziXunBean.getData().getList();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mView.onGetDataSuccess(ziXunBeanList);
                    }
                });
            }

            @Override
            public void onFailure(Call<ZiXunBean> call, Throwable t) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mView.onGetDataFail("网络连接失败");
                    }
                });
            }
        });
    }

}
